/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.modules.biz.domain.Vidicon;
import me.zhengjie.modules.biz.service.VidiconService;
import me.zhengjie.modules.biz.service.dto.VidiconQueryCriteria;
import me.zhengjie.utils.FileUtil;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "vidicon管理")
@RequestMapping("/api/vidicon")
public class VidiconController {

    private final VidiconService vidiconService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('vidicon:list')")
    public void exportVidicon(HttpServletResponse response, VidiconQueryCriteria criteria) throws IOException {
        vidiconService.download(vidiconService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询vidicon")
    @ApiOperation("查询vidicon")
//    @PreAuthorize("@el.check('vidicon:list')")
    public ResponseEntity<Object> queryVidicon(VidiconQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(vidiconService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增vidicon")
    @ApiOperation("新增vidicon")
//    @PreAuthorize("@el.check('vidicon:add')")
    public ResponseEntity<Object> createVidicon(@Validated @RequestBody Vidicon resources){
        return new ResponseEntity<>(vidiconService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改vidicon")
    @ApiOperation("修改vidicon")
//    @PreAuthorize("@el.check('vidicon:edit')")
    public ResponseEntity<Object> updateVidicon(@Validated @RequestBody Vidicon resources){
        vidiconService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除vidicon")
    @ApiOperation("删除vidicon")
//    @PreAuthorize("@el.check('vidicon:del')")
    public ResponseEntity<Object> deleteVidicon(@RequestBody Integer[] ids) {
        vidiconService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

//    @Log("下载图片")
//    @ApiOperation("下载图片")
//    @PostMapping(value = "/downloadPicture")
//    @PreAuthorize("@el.check('vidicon:list')")
//    public ResponseEntity<Object> downloadPicture(@RequestParam MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response) throws IOException {
//        vidiconService.downloadPicture(FileUtil.toFile(multipartFile), request, response);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }

    @Log("下载图片")
    @ApiOperation("下载图片")
    @GetMapping(value = "/downloadPicture")
    @AnonymousAccess
    public ResponseEntity<Object> downloadPicture(@RequestParam String urlString, HttpServletResponse response) throws IOException {
        FileUtil.downloadPhoto(urlString, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("下载视频")
    @ApiOperation("下载视频")
    @GetMapping(value = "/downloadVideo")
    @AnonymousAccess
    public ResponseEntity<Object> downloadVideo(@RequestParam String urlString, HttpServletResponse response) throws IOException {
        FileUtil.downloadVideo(urlString, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/queryVidiconFor3d")
    @Log("查询摄像机")
    @ApiOperation("查询摄像机")
    @AnonymousAccess
    public ResponseEntity<Object> queryVidiconFor3d(VidiconQueryCriteria criteria){
        return new ResponseEntity<>(vidiconService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping("/3d/listFloor")
    @AnonymousAccess
    public ResponseEntity<Object> listFloor(Integer floor){
        return new ResponseEntity<>(vidiconService.listFloor(floor),HttpStatus.OK);
    }

    @AnonymousGetMapping("/3d/getCountByFloor")
    public ResponseEntity<Object> getCountByFloor(){
        return new ResponseEntity<>(vidiconService.getCountByFloor(),HttpStatus.OK);
    }
}
